package cn.loveq.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import cn.loveq.android.manager.SinaWeiboAuthorize;
import cn.loveq.android.widget.FlingGallery;
import cn.loveq.android.widget.InteractiveGalleryViewItem;
import cn.loveq.android.widget.RadioGalleryViewItem;
import cn.loveq.android.widget.WeiboGalleryViewItem;

import com.waps.AppConnect;

/**
 * 
 * 
 * 项目名称：loveq4Weibo 类名称：MainActivity 类描述：主界面 创建人：李伟豪 创建时间：2011-12-5 下午03:16:40
 * 
 * @version
 * 
 */
public class MainActivity extends Activity {
	private final int color_red = Color.argb(100, 200, 0, 0);
	private final int color_green = Color.argb(100, 0, 200, 0);
	private final int color_blue = Color.argb(100, 0, 0, 200);

	private final String[] mLabelArray = { "Raido界面", "互动空间" };
	private final int[] mColorArray = { color_red, color_green, color_blue };

	private FlingGallery mGallery;
	private SinaWeiboAuthorize authorize;
	private RadioGalleryViewItem radioGalleryViewItem;
	private InteractiveGalleryViewItem interactiveGalleryViewItem;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);

		// 初始化统计器，并通过代码设置WAPS_ID, WAPS_PID
		AppConnect
				.getInstance(Constants.WAPS_ID, Constants.WAPS_PID_GFAN, this);

	//	AppConnect.getInstance("09f277ca386ee99cb4c910e09f562112","WAPS",this);
		
		
		// 使用自定义的OffersWebView
		AppConnect.getInstance(this).setAdViewClassName(
				"cn.loveq.android.widget.MyAdView");

		// 禁用错误报告
		AppConnect.getInstance(this).setCrashReport(false);

		mGallery = new FlingGallery(this);
		mGallery.setBackgroundResource(R.drawable.bg);
		mGallery.setPaddingWidth(5);
		mGallery.setIsGalleryCircular(false);

		// 检查是否有绑定微博
		radioGalleryViewItem = new RadioGalleryViewItem(MainActivity.this);
		interactiveGalleryViewItem = new InteractiveGalleryViewItem(
				MainActivity.this);
		authorize = new SinaWeiboAuthorize(this);
		authorize.setInteractiveGalleryViewItem(interactiveGalleryViewItem);
		if (!authorize.checkAuthToken()) {
			// authorize.authorizeForSina();
			authorize.showAuthSina(null);
			Constants.isBoundWeibo = false;
		} else {
			authorize.reLoaderAuthToken();
			// authorize.getFriendshipInfo(Long.parseLong(Constants.HUGO_UID));
			// authorize.getFriendshipInfo(Long.parseLong(Constants.ZHI_UID));
			// authorize.getUserTimeLine(Long.parseLong(Constants.HUGO_UID), 1,
			// 1);
			// authorize.showBatch("3437685647861297",3,1);
			// authorize.createComments("3437685647861297", "测试用1111", 1);
			// authorize.commentsReply("3437713581756653", "3437685647861297",
			// "再测试111",1);
		}
		mGallery.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
				android.R.layout.simple_list_item_1, mLabelArray) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				Log.d(Constants.TAG, "count=" + position);
				switch (position) {
				case 0:
					/*
					 * return new WeiboGalleryViewItem(getApplicationContext(),
					 * position);
					 */
					return radioGalleryViewItem;
				case 1:
					if (interactiveGalleryViewItem != null) {
						interactiveGalleryViewItem.loadData();
					}
					return interactiveGalleryViewItem;
				case 2:
				default:
					break;
				}
				return new WeiboGalleryViewItem(getApplicationContext(),
						position);
			}
		});

		LinearLayout layout = new LinearLayout(getApplicationContext());
		layout.setOrientation(LinearLayout.VERTICAL);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);

		layoutParams.setMargins(10, 10, 10, 10);
		layoutParams.weight = 1.0f;

		layout.addView(mGallery, layoutParams);

		setContentView(layout);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& keyCode == KeyEvent.KEYCODE_BACK) {
			showExitDialog();
		}
		return super.onKeyDown(keyCode, event);
	}

	private void showExitDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.exit)
				.setMessage(R.string.exit_msg)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (radioGalleryViewItem != null
										&& radioGalleryViewItem.isStoped()) {
									radioGalleryViewItem.stop();
								}
								finish();
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						}).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "关于");
		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "退出");
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Intent intent = new Intent(MainActivity.this, AboutActivity.class);
			startActivity(intent);
			break;
		case Menu.FIRST + 2:
			showExitDialog();
			break;
		}
		return false;

	}

	@Override
	protected void onDestroy() {
		AppConnect.getInstance(this).finalize();
		super.onDestroy();
	}

}
